package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.vo.CountByTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-16
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }


    /**
     * 获取已取消预约数量
     * @return
     */
    @Override
    public Integer getCancelledReservationCount() {
        // 获取一天的时间间隔
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        Long userId = UserThreadLocal.getUserId();
        List<Reservation> reservations= reservationMapper.getCancelledReservationCount(userId,startTime,endTime);

        return reservations.size();
    }

    /**
     * 获取当天剩余的预约数量
     * @param time
     * @return
     */
    @Override
    public List<CountByTimeVo> getCountByTime(long time) {
        List<CountByTimeVo> countByTimeVos =new ArrayList<>();

        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        // 获取当前日期
        LocalDateTime startDateTime = LocalDateTime.now().with(LocalTime.of(8, 0, 0)); // 当天开始时间
        LocalDateTime endDateTime = LocalDateTime.now().with(LocalTime.of(18,0,0));   // 当天结束时间
        LocalDateTime startTime = LocalDateTime.now();
        LocalDateTime endTime = null;
        if(startDateTime.isBefore(startTime)){
            endTime = startTime.plusMinutes(30);
            startTime = endTime;
            CountByTimeVo countByTimeVo = reservationMapper.getCountByTime(startTime,endTime);
            countByTimeVos.add(countByTimeVo);
        }
        return countByTimeVos;
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @Override
    public int cancelReservation(Long id) {
        Reservation reservation = getOne(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getId, id));
        if(ObjectUtils.isEmpty(reservation)){
            return 0;
        }
        reservation.setStatus(2);
        reservation.setUpdateTime(DateUtils.getNowDate());
        int i = reservationMapper.updateReservation(reservation);
        return i;
    }

}
